//
//  TestViewController.m
//  全局变量
//
//  Created by YYY on 2018/1/13.
//  Copyright © 2018年 YYY. All rights reserved.
//

#import "TestViewController.h"

static NSString * const singer = @"全民K歌";

typedef void(^RequestSuccess)(NSString *result);

typedef void(^RequestFail)(NSString *error);

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"singer = %@", singer);
    
    //block回调：
    [self method1:^(NSString *result) {
        NSLog(@"结果1：%@", result);
    }];
    
    [self method2:@"iPhone" complection:^(NSString *result) {
        NSLog(@"结果2：%@", result);
    }];
    
    [self method3:^(NSString *result) {
        NSLog(@"结果3：%@", result);
    } fail:^(NSString *error) {
        NSLog(@"出错1：%@", error);
    }];
    
    [self method4:@"Good luck today!" success:^(NSString *result) {
        NSLog(@"结果4：%@", result);
    } fail:^(NSString *error) {
        NSLog(@"出错2：%@", error);
    }];
}

- (void)method1:(void(^)(NSString *result))complection
{
    NSString *string = @"Happy New Year!";
    complection(string);
}

- (void)method2:(NSString *)str complection:(void(^)(NSString *result))complection
{
    NSString *string = @"What a nice day today!";
    complection(string);
}

- (void)method3:(RequestSuccess)success fail:(RequestFail)fail
{
    NSString *string = @"I'm in a good mood today!";
    if (string.length > 0)
    {
        success(string);
    }
    else
    {
        fail(@"网络出错");
    }
}

- (void)method4:(NSString *)str success:(RequestSuccess)success fail:(RequestFail)fail
{
    NSString *string = str;
    if (string.length > 0)
    {
        success(string);
    }
    else
    {
        fail(@"网络出错");
    }
}

@end
